<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
use Hyperf\Server\Event;
use Hyperf\Server\Server;
use Swoole\Constant;

$http =         [
    'name'      => 'http',
    'type'      => Server::SERVER_HTTP,
    'host'      => '0.0.0.0',
    'port'      => env('HTTP_PORT', 9505),
    'sock_type' => SWOOLE_SOCK_TCP,
    'callbacks' => [
        Event::ON_REQUEST => [Hyperf\HttpServer\Server::class, 'onRequest'],
    ],
];

//$ws = [
//    'name'      => 'ws',
//    'type'      => Server::SERVER_WEBSOCKET,
//    'host'      => '0.0.0.0',
//    'port'      => env('WS_PORT', 9502),
//    'sock_type' => SWOOLE_SOCK_TCP,
//    'callbacks' => [
//        Event::ON_REQUEST    => ['HttpServer', 'onRequest'],
//        Event::ON_HAND_SHAKE => [Hyperf\WebSocketServer\Server::class, 'onHandShake'],
//        Event::ON_MESSAGE    => [Hyperf\WebSocketServer\Server::class, 'onMessage'],
//        Event::ON_CLOSE      => [Hyperf\WebSocketServer\Server::class, 'onClose'],
//    ],
//];

$admin = [
    'name'      => 'admin',
    'type'      => Server::SERVER_HTTP,
    'host'      => '0.0.0.0',
    'port'      => env('ADMIN_PORT', 9506),
    'sock_type' => SWOOLE_SOCK_TCP,
    'callbacks' => [
        Event::ON_REQUEST => ["AdminServer", 'onRequest'],
    ],
];

$wsServer =  env('WS_SERVER', 0);
$adminServer =  env('ADMIN_SERVER', 1);
$httpServer =  env('HTTP_SERVER', 1) || (!$wsServer && !$adminServer);

return [
    'mode'    => SWOOLE_PROCESS,
    'servers' => [
        ...$httpServer ? [$http] : [],
//        ...$wsServer ? [$ws] : [],
        ...$adminServer ? [$admin] : [],
    ],

    'settings'  => [
        Constant::OPTION_ENABLE_COROUTINE    => true,
        Constant::OPTION_WORKER_NUM          => env('WORKER_NUM', swoole_cpu_num()),
        Constant::OPTION_PID_FILE            => env('PID_FILE', BASE_PATH . '/runtime/hyperf.pid'),
        Constant::OPTION_OPEN_TCP_NODELAY    => true,
        Constant::OPTION_MAX_COROUTINE       => 100000,
        Constant::OPTION_OPEN_HTTP2_PROTOCOL => true,

        Constant::OPTION_MAX_REQUEST        => 100000,
        Constant::OPTION_SOCKET_BUFFER_SIZE => 2 * 1024 * 1024,
        Constant::OPTION_BUFFER_OUTPUT_SIZE => 2 * 1024 * 1024,
        Constant::OPTION_PACKAGE_MAX_LENGTH => 100 * 1024 * 1024,
        'task_worker_num' => env('WORKER_NUM', swoole_cpu_num()),  // Task Worker 数量，根据您的服务器配置而配置适当的数量
        'task_enable_coroutine' => false, // 因为 `Task` 主要处理无法协程化的方法，所以这里推荐设为 `false`，避免协程下出现数据混淆的情况
        'document_root' => BASE_PATH . '/public',
        'enable_static_handler' => true,//是否允许启动静态处理,如果存在会直接发送文件内容给客户端
    ],
    'callbacks' => [
        Event::ON_WORKER_START => [Hyperf\Framework\Bootstrap\WorkerStartCallback::class, 'onWorkerStart'],
        Event::ON_PIPE_MESSAGE => [Hyperf\Framework\Bootstrap\PipeMessageCallback::class, 'onPipeMessage'],
        Event::ON_WORKER_EXIT  => [Hyperf\Framework\Bootstrap\WorkerExitCallback::class, 'onWorkerExit'],
        // Task callbacks
        Event::ON_TASK         => [Hyperf\Framework\Bootstrap\TaskCallback::class, 'onTask'],
        Event::ON_FINISH       => [Hyperf\Framework\Bootstrap\FinishCallback::class, 'onFinish'],
    ],
];